package com.huang.config;

import com.huang.util.JwtUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        String token=request.getHeader("Authentication");
        //判定请求中是否有令牌
        if(token==null||"".equals(token))
            throw new RuntimeException("please login");
        //判定令牌是否已经过期
        boolean flag= JwtUtils.isTokenExpired(token);
        if(flag)
            throw new RuntimeException("login timeout,please login");
        return true;//true表示放行,false表示拦截到请求以后,不再继续传递

    }
}
